using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pokemon
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Soul : DrawableGameComponent, ITappedInner
    {       
        private Vector2 _position;        

        private Sprite _sprite;        

        private static int _delay;

        protected TimeSpan _delayTimeSpan = TimeSpan.FromSeconds(50);//Thuoc tinh dung de tinh toan thoi gian delay sau 1 loat dan

        protected TimeSpan _TimeSpan;//Thuoc tinh dung de lam giam toc do thay doi khung hinh cua unit

        protected int _frameIndex;

        public Soul(Game game, Vector2 position) 
            : base(game)
        {            
            _position = position;
            Initialize();
            NormalizePosition();
        }

        public override void Initialize()
        {
            XDocument doc = XDocument.Load(@"Content/Textures/Special/Soul/SoulDef.xml");
            Texture2D texture = null;
            Vector2 frameSize = Vector2.Zero;
            Vector2 centerPoint = Vector2.Zero;
            int numFrame = 0;
            if (doc.Document != null)
            {
                //get delay time(s)
                XName name = XName.Get("Delay");
                var definitions = doc.Document.Descendants(name);
                _delay = Int32.Parse((definitions.First()).Value); 
                // get properties
                name = XName.Get(@"Definition");
                definitions = doc.Document.Descendants(name);

                foreach (var def in definitions)
                {
                    var xAttribute = def.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        texture = Game.Content.Load<Texture2D>(xAttribute.Value);
                                                
                        xAttribute = def.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = float.Parse(xAttribute.Value);
                            xAttribute = def.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = float.Parse(xAttribute.Value);
                                frameSize = new Vector2(width, height);
                            }
                        }
                        
                    }

                    xAttribute = def.Attribute(@"SplitFrame");
                    numFrame = Int32.Parse(xAttribute.Value);

                    xAttribute = def.Attribute(@"XCenterPoint");

                    var xCenterPoint = Int32.Parse(xAttribute.Value);
                    xAttribute = def.Attribute(@"YCenterPoint");

                    var yCenterPoint = Int32.Parse(xAttribute.Value);

                    centerPoint = new Vector2(xCenterPoint, yCenterPoint);
                    _sprite = new Sprite(texture, numFrame, frameSize, centerPoint, false);
                }

            }
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            _delayTimeSpan += gameTime.ElapsedGameTime;
            _TimeSpan += gameTime.ElapsedGameTime;
            if (_TimeSpan.Milliseconds < 200)
                return;
            base.Update(gameTime);
            _TimeSpan = TimeSpan.Zero;            

            _sprite.Update();            
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sp = (SpriteBatch)Game.Services.GetService(typeof (SpriteBatch));
            _sprite.Draw(sp, _position, Color.White);            
        }

        void NormalizePosition()
        {
            
        }

        public bool CheckTapped(Point p)
        {
            Rectangle rect = new Rectangle((int)_position.X, (int)_position.Y, (int)_sprite.Size.X, (int)_sprite.Size.Y);
            if (rect.Contains(p))
            {
                SoulManager soulManager = (SoulManager) Game.Services.GetService(typeof (SoulManager));
                soulManager.Remove(this);
                return true;
            }
            return false;
        }
    }
}
